

package com.haoxi.gateway.cngpmsg;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;


public class CNGPLoginMessage extends CNGPMessage
{

    private StringBuffer strBuf;

    public CNGPLoginMessage(String clientId, String shared_Secret, int loginMode, Date timeStamp, int version)
        throws IllegalArgumentException
    {
        if(clientId == null)
            throw new IllegalArgumentException("The parameter of login message is wrong:clientId  value is null.");
        if(clientId.length() > 10)
            throw new IllegalArgumentException(
            "The parameter of login message is wrong:clientId length is larger than 10");
        if(shared_Secret.length() > 15)
            throw new IllegalArgumentException(
            "The parameter of login message is wrong:shared_Secret length is larger than 15");
        if(loginMode < 0 || loginMode > 255)
            throw new IllegalArgumentException(
            "The parameter of login message is wrong:loginMode less than 0 or larger than 255.");
        if(version < 0 || version > 255)
            throw new IllegalArgumentException(
            "The parameter of login message is wrong:version less than 0 or larger than 255.");
        int len = 48;
        buf = new byte[len];
        setMsgLength(len);
        setRequestId(CNGPCommon.CNGP_LOGIN);
        System.arraycopy(clientId.getBytes(), 0, buf, 16, clientId.length());
        if(shared_Secret != null)
            len = clientId.length() + 17 + shared_Secret.length();
        else
            len = clientId.length() + 17;
        byte tmpbuf[] = new byte[len];
        int tmploc = 0;
        System.arraycopy(clientId.getBytes(), 0, tmpbuf, 0, clientId.length());
        tmploc = clientId.length() + 7;
        if(shared_Secret != null)
        {
            System.arraycopy(shared_Secret.getBytes(), 0, tmpbuf, tmploc, shared_Secret.length());
            tmploc += shared_Secret.length();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");
        String strTimeStamp = dateFormat.format(timeStamp);
        System.arraycopy(strTimeStamp.getBytes(), 0, tmpbuf, tmploc, 10);
        md5(tmpbuf, 0, len, buf, 26);
        buf[42] = (byte)loginMode;
        TypeConvert.int2byte(Integer.parseInt(strTimeStamp), buf, 43);
        buf[47] = (byte)version;
        strBuf = new StringBuffer();
        strBuf.append(",clientId=").append(clientId);
        strBuf.append(",shared_Secret=").append(shared_Secret);
        strBuf.append(",loginMode=").append(loginMode);
        strBuf.append(",timeStamp=").append(timeStamp);
        strBuf.append(",version=").append(version);
    }

    public String toString()
    {
        StringBuffer outStr = new StringBuffer();
        outStr.append("CNGPLoginMessage:");
        outStr.append("PacketLength=").append(buf.length);
        outStr.append(",RequestID=1");
        outStr.append(",SequenceId=").append(getSequenceId());
        if(strBuf != null)
            outStr.append(strBuf.toString());
        return outStr.toString();
    }
    public static void md5(byte data[], int offset, int length, byte digest[],
            int dOffset) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data, offset, length);
            md5.digest(digest, dOffset, 16);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (DigestException ex) {
            ex.printStackTrace();
        }
    }
}
